
/*
 * Copyright (C) 2008-2009 Archie L. Cobbs. All rights reserved.
 *
 * $Id: AbstractSchemaUpdate.java 377 2010-03-11 20:50:18Z archie.cobbs $
 */

package org.dellroad.sidekar.schema.change;

import org.dellroad.sidekar.Sidekar;
import org.dellroad.sidekar.schema.model.EntityModel;
import org.dellroad.sidekar.schema.model.SchemaModel;

/**
 * Superclass for {@link SchemaChange} classes that are specific to an entity.
 */
public abstract class EntityChange extends SchemaChange {

    private final String entityName;

    EntityChange(String entityName) {
        Sidekar.checkValidEntityName(entityName);
        this.entityName = entityName;
    }

    public String getEntityName() {
        return this.entityName;
    }

    @Override
    public void checkValidFor(SchemaModel schemaModel) {
        this.checkValidEntityName(this.entityName);
    }

    // Describe an entity relative to a descendant or ancestor (possibly the same)
    String getDescription(EntityModel entityModel, EntityModel from) {
        String desc = "" + entityModel;
        if (entityModel != from) {
            if (entityModel.isAncestorOf(from))
                desc += " (ancestor of " + from + ")";
            else if (from.isAncestorOf(entityModel))
                desc += " (descendant of " + from + ")";
            else
                desc += " (unrelated to " + from + ")";
        }
        return desc;
    }
}

